let DeclarativeEnvironmentRecords = require('./DeclarativeEnvironmentRecords')
let ObjectEnvironmentRecords = require('./ObjectEnvironmentRecords')
/* 词法环境由：一个环境记录项和一个外部词法的引用组成 */
class LexicalEnvironment{
    constructor(environmentRecord,outer){
        this.environmentRecord = environmentRecord;
        this.outer = outer
    }
    //创建变量 登记变量
    createBinding(N){
        return this.environmentRecord.createBinding(N)
    }
    //设置某个变量值
    setBinding(N,V){
        this.environmentRecord.setBinding(N,V)
    }
    //判断某个变量已经是否登陆过了
    hasBinding(){
        return this.environmentRecord.hasBinding(N)
    }
    //获取变量的值
    getBindingValue(N){
        return this.environmentRecord.getBindingValue[N]
    }
    getIdentifierReference(name){
        this.lexicalEnvironment = this;
        do{
            //判断当前的词法环境有没有定义name这个变量
            let exists = this.lexicalEnvironment.hasBinding(name)
            if(exists){
                return this.lexicalEnvironment.getBindsValue(name)
            }else{
                this.lexicalEnvironment = lexicalEnvironment.outer;
            }
        }while(this.lexicalEnvironment)
    }
    static NewDeclarativeEnvironment(outerLexicalEnvironment){
        let envRec = new DeclarativeEnvironmentRecords();//里面不包含任何变量绑定
        let env = new LexicalEnvironment(envRec,outerLexicalEnvironment);
        return env
    }
    static NewObjectEnvironment(object,outerLexicalEnvironment){
        let envRec = new ObjectEnvironmentRecords();
        let env = new LexicalEnvironment(envRec,outerLexicalEnvironment);
        return env
    }
}
module.exports = LexicalEnvironment